//
//  ZKHttpManager.m
//  ZKCompetitionApp
//
//  Created by 凯朱 on 2019/8/24.
//

#import "ZKHttpManager.h"

@implementation ZKHttpManager
//上传文件
+(void)uploadFilesWithParams:(NSData *)fileData finished:(ZCRequiredFinished)finished{
    NSString *urlStr = [NSString stringWithFormat:@"%@/upload/uploadFile",URL_main];
    [YQNetworking uploadFileWithUrl:urlStr fileData:fileData type:@"png" name:@"file" mimeType:@"" progressBlock:nil successBlock:^(id response) {
        if (finished) finished(YES,response,@"0",@"上传文件成功");
    } failBlock:^(NSError *error) {
        if (finished) finished(NO,@{},@"-1",@"上传文件失败");
    }];
}

+(void)downloadFilesWithUrl:(NSString *)fileUrl finished:(ZCRequiredFinished)finished{
    [YQNetworking downloadWithUrl:fileUrl progressBlock:nil successBlock:^(NSURL *url) {
        if (finished) finished(YES,url.absoluteString,@"0",kLocalString(@"Download success"));
    } failBlock:^(NSError *error) {
        if (finished) finished(NO,@{},@"-1",kLocalString(@"Download faild"));
    }];
}

+(void)postWithPath:(NSString *)path params:(NSDictionary *)params finished:(ZCRequiredFinished)finished{
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",URL_main,path];
    [ZCBaseRequestManager requestWithUrl:urlStr method:POST params:params finished:^(BOOL isSuccessed, id  _Nonnull result, NSString * _Nonnull code, NSString * _Nonnull message) {
        if (finished) finished(isSuccessed,result,code,message);
    }];
}

+(void)postWithPath:(NSString *)path timeout:(NSInteger)timeout params:(NSDictionary *)params finished:(ZCRequiredFinished)finished{
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",URL_main,path];
    [ZCBaseRequestManager requestWithUrl:urlStr method:POST timeout:timeout params:params finished:^(BOOL isSuccessed, id  _Nonnull result, NSString * _Nonnull code, NSString * _Nonnull message) {
        if (finished) finished(isSuccessed,result,code,message);
    }];
}

+(void)getWithPath:(NSString *)path params:(NSDictionary *)params finished:(ZCRequiredFinished)finished{
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",URL_main,path];
    [ZCBaseRequestManager requestJsonWithUrl:urlStr method:GET params:params finished:^(BOOL isSuccessed, id  _Nonnull result, NSString * _Nonnull code, NSString * _Nonnull message) {
        if (finished) finished(isSuccessed,result,code,message);
    }];
}

+(void)getWithPath:(NSString *)path timeout:(NSInteger)timeout params:(NSDictionary *)params finished:(ZCRequiredFinished)finished{
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",URL_main,path];
    [ZCBaseRequestManager requestJsonWithUrl:urlStr method:GET timeout:timeout params:params finished:^(BOOL isSuccessed, id  _Nonnull result, NSString * _Nonnull code, NSString * _Nonnull message) {
        if (finished) finished(isSuccessed,result,code,message);
    }];
}

+(void)deleteWithPath:(NSString *)path params:(NSDictionary *)params finished:(ZCRequiredFinished)finished{
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",URL_main,path];
    [ZCBaseRequestManager deleteWithUrl:urlStr params:params finished:^(BOOL isSuccessed, id  _Nonnull result, NSString * _Nonnull code, NSString * _Nonnull message) {
        if (finished) finished(isSuccessed,result,code,message);
    }];
}



/*
 上报开锁记录
 unlockType: 1-蓝牙开锁  2-蓝牙远程开锁  3-指纹开锁
 unlockTime - 开锁时间戳
 unlockUid - 开锁成员id
 unlockUame - 开锁成员名称
 unlockId - 开锁id
 deviceId - 设备id
 */
+(void)uploadOpenLockRecord:(NSString *)unlockType
                 unlockTime:(NSString *)unlockTime
                  unlockUid:(NSString *)unlockUid
                 unlockUame:(NSString *)unlockUame
                   unlockId:(NSString *)unlockId
                   deviceId:(NSString *)deviceId
                     finish:(void(^)(BOOL isSuccess))finish{
    if (unlockTime.length == 0) unlockTime = [[NSDate date] timestamp];
    NSDictionary *params = @{@"unlockType":unlockType,
                             @"unlockTime":unlockTime,
                             @"unlockUid":unlockUid,
                             @"unlockId":unlockId,
                             @"deviceId":kCurClickDevice.deviceId,
                             @"unlockUame":unlockUame};
    [ZKHttpManager postWithPath:@"/msl_lock_key/UnlockLog/add" params:params finished:^(BOOL isSuccessed, id  _Nonnull result, NSString * _Nonnull code, NSString * _Nonnull message) {
        if (isSuccessed) {
            kNotifyRefreshDevcieList;
        }
        if (finish) finish(isSuccessed);
    }];
 
}

/*
 批量上报开锁/告警记录- 同步用到
 */
+(void)batchUploadLogsWith:(NSArray <SLLockLogModel *>*)openLogs warnLogs:(NSArray <SLLockLogModel *>*)warnLogs finish:(void(^)(BOOL isSuccess))finish{
    NSMutableArray *mslUnlockLogList = [NSMutableArray new];
    [openLogs enumerateObjectsUsingBlock:^(SLLockLogModel *model, NSUInteger idx, BOOL * _Nonnull stop) {
        NSDictionary *openInfo = @{@"unlockType":kSTRING(model.unlockType),
                                   @"unlockTime":kSTRING(model.unlockTime),
                                   @"accessId":kSTRING(model.accessId),
                                   @"deviceId":kCurClickDevice.deviceId};
        [mslUnlockLogList addObject:openInfo];
    }];
    
    [warnLogs enumerateObjectsUsingBlock:^(SLLockLogModel *model, NSUInteger idx, BOOL * _Nonnull stop) {
        NSDictionary *warnInfo = @{@"warnType":kSTRING(model.warnType),
                                   @"unlockTime":kSTRING(model.unlockTime),
                                   @"deviceId":kCurClickDevice.deviceId};
        [mslUnlockLogList addObject:warnInfo];
    }];
    NSDictionary *params = @{@"mslUnlockLogList":mslUnlockLogList,
                             @"deviceId":kCurClickDevice.deviceId};
    [ZKHttpManager postWithPath:@"/msl_lock_key/SyncInfo" params:params finished:^(BOOL isSuccessed, id  _Nonnull result, NSString * _Nonnull code, NSString * _Nonnull message) {
        if (finish) finish(isSuccessed);
    }];
    
}

//同步设备数据
+(void)sysncDeviceDataWith:(NSDictionary *)params finished:(ZCRequiredFinished)finished{
    NSMutableDictionary *newParams = [[NSMutableDictionary alloc] initWithDictionary:params];
    if (kSTRING(params[@"deviceNo"]).length > 0) {
        [newParams setObject:kSTRING(params[@"deviceNo"]) forKey:@"deviceNo"];
        [newParams setObject:kSTRING(params[@"deviceId"]) forKey:@"deviceId"];
    }else{
        [newParams setObject:kCurClickDevice.deviceNo forKey:@"deviceNo"];
        [newParams setObject:kCurClickDevice.deviceId forKey:@"deviceId"];
    }
    [ZKHttpManager postWithPath:@"/msl_user/SyncInfo" params:newParams finished:^(BOOL isSuccessed, id  _Nonnull result, NSString * _Nonnull code, NSString * _Nonnull message) {
        if (finished) finished(isSuccessed,result,code,message);
    }];
}

//同步开锁方式数据 - 不包含指纹数据
+(void)sysncKeyDataWith:(SLLockKeyModel *)keyModel finished:(ZCRequiredFinished)finished{
    NSDictionary *params = @{@"keyId":keyModel.keyId,
                             @"status":kSTRING(keyModel.status),
                             @"keyType":kSTRING(@(keyModel.keyType)),
                             @"keyName":keyModel.keyName,
                             @"startTime":keyModel.startTime,
                             @"endTime":keyModel.endTime,
                             @"weekDays":[ZCTool objectToJson:keyModel.weekDays],
                             @"dayStart":keyModel.dayStart,
                             @"dayEnd":keyModel.dayEnd,
                             @"deviceId":kCurClickDevice.deviceId};
    
    [ZKHttpManager postWithPath:@"/msl_lock_key/updateKey" params:params finished:^(BOOL isSuccessed, id  _Nonnull result, NSString * _Nonnull code, NSString * _Nonnull message) {
        if (finished) finished(isSuccessed,result,code,message);
    }];
}




@end
